#include<reg52.h>
#include<intrins.h>

#define uchar unsigned char 
#define uint unsigned int 

void delay(uchar ms);    
void wr_com(unsigned char com);    
void wr_dat(unsigned char dat);    
void xianshi(uchar dizhi, uchar suzi);   
uchar TLC549_ADC(void);   
void uart_init();         

sbit rs = P2^0;
sbit rw = P2^1;
sbit e = P2^2;

sbit DI  = P1^0;
sbit CS  = P1^1;
sbit CLK = P1^2;

uchar ad_0;
uint AD;


void main()
{
	uart_init();
	wr_com(0x01);    
	wr_com(0x38);   
    wr_com(0x0c);     
	while(1)
	 {
	  delay(100);
	  ad_0=TLC549_ADC();
	  AD=ad_0*1.97;
	  xianshi(0x80+2,'v');
	  xianshi(0x80+3,'=');
	  xianshi(0x80+5,0x30+AD%1000/100);
	  xianshi(0x80+6,'.');
	  xianshi(0x80+7,0x30+AD%100/10);
	  xianshi(0x80+8,0x30+AD%10);
	  xianshi(0x80+9,'V');
	}
}

void delay(uchar ms)    
{       
	 uchar i ;
	 while(ms--)
	 {
	  	for(i = 0 ; i<250;i++) ;
	 }
}


void wr_com(unsigned char com)   
{       
      
 	rs = 0 ;
  	rw = 0 ;
  	e = 0 ;
  	P0 = com ;
	delay(1);
  	e = 1 ;
	delay(1);
  	e = 0 ; 
}		
	   

void wr_dat(unsigned char dat)
{      
      
 	rs = 1 ;
  	rw = 0 ;
  	e = 0 ;
  	P0 = dat ;
    delay(1);
  	e = 1 ;
	delay(1);
  	e = 0 ; 
}


void xianshi(uchar dizhi, uchar suzi)
{ 
     wr_com(dizhi);
	 wr_dat(suzi);
}


uchar TLC549_ADC(void)
{
	   uchar n, tmp;

        CS  = 1;                     
        CLK = 0;

        CS  = 0;                     
        _nop_();
        _nop_();                     

        for(n = 0; n < 8; n++) 
        { 
                tmp <<= 1;
                tmp |=  DI;

                CLK = 1;        
                _nop_();        
                CLK = 0;        
        }
        CS = 1;            
        for(n = 17; n != 0; n--) _nop_();  
        return (tmp);

}


void uart_init()
{
	TMOD=0x20;		
	SCON=0x50;
	TH1=0xFD;
	TL1=0xFD;
	TR1=1;
	EA=1;
}
